<?php

namespace App\Admin\Controllers;

use App\Models\User;
use App\Models\UserApplyCash;

use App\TraitHelper\RequestHelper;
use Collective\Html\HtmlBuilder;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;

class UserCashController extends Controller
{
    use ModelForm,RequestHelper;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        
        return Admin::content(function (content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(UserApplyCash::class, function (Grid $grid) {
            $grid->model()->orderBy('id','desc');
            $grid->id('ID')->sortable();

            $grid->user_id("用户ID");
            $grid->column('', "用户手机")->display(function (){
                $model = User::find($this->user_id);
                return $model? $model->mobile:'';
            });
            $grid->alipay("提现支付宝");
            $grid->status("提现状态")->display(function ($status) {
                $map[UserApplyCash::STATUS_SUCCESS] = '审核通过';
                $map[UserApplyCash::STATUS_FAIL] = '审核未通过';
                $map[UserApplyCash::STATUS_WAIT] = '等待审核';
                return isset($map[$status]) ? $map[$status] : '等待审核';
            });;
            $grid->disableCreation();
            $grid->disableBatchDeletion();
            $that = $this;
            $grid->actions(function(Grid\Displayers\Actions $actions)use($that){
                $actions->disableDelete();
                $actions->disableEdit();
                $url = route('admin.userCash.success', ['id' => $actions->getKey()]);
                $makeConfirmJs = $that->makeConfirmUrl($url, "审核通过");
                $actions->append(\Html::tag('a',"审核通过",['href'=>$makeConfirmJs]));
            });

            $grid->column('', '用户手机')->display(function(){
                return User::find($this->user_id)->mobile;
            });
            $grid->filter(function(Grid\Filter $filter){
                // 禁用id查询框
                $filter->disableIdFilter();
                $filter->where(function ($query) {
                    $user = User::where('mobile',$this->input)->first();
                    $query->where('user_id',$user->id);

                }, '手机号');
            });
//            $grid->order_id_list("订单号");
            $grid->created_at('创建时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(UserApplyCash::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
    public function success(Request $request){
        $id = $request->input('id');
        if ($id) {
            $u = UserApplyCash::find($id);
            $u->status = UserApplyCash::STATUS_SUCCESS;
            $u->save();
            admin_toastr("处理成功", 'success');

        }else{
            admin_toastr("处理失败!", 'error');

        }
        return back();
    }
}
